﻿using System;
using System.Collections.Generic;
using System.Text;
using Vortex.Drawing;
using Vortex;
using Vortex.SceneFramework;
using Vortex.Input;
using Vortex.Debugging;

namespace Vortex.Demo.Pacman {

	/// <summary>
	/// Implements Pacman level domain
	/// </summary>
    public class LevelScene : Scene {
		[ElementRef("PacmanWorld")]
		private GameWorld _world;

		[ElementRef]
		private TextElement StartLevelLabel = null;

		/// <summary>
		/// Starts the level for this domain.
		/// </summary>
		/// <param name="level">The level number.</param>
		public void BeginGame(int level) {
			//show this domain
			Game.Screen.SetScene(this, SceneSwitchEffects.Fade, 0.8f);
			_world.IsEnabled = false;
			_world.StartLevel(1);
			StartLevel(level);
		}

		private void StartLevel(int level) {

			StartLevelLabel.Text = "Level " + level;
			StartLevelLabel.IsVisible = true;
			StartLevelLabel.Location = new Vector2(-400, 300);

			//show Level N label effect
			TimeLine
				.Repeat(0.8f, s => StartLevelLabel.LocationX = -400 + (s.Progress * s.Progress) * 800)
				.Wait(1.5f)
				.Invoke(() => _world.IsEnabled = true)
				.Repeat(0.8f, s => StartLevelLabel.LocationX = 400 + (s.Progress * s.Progress) * 800)
				.Invoke(() => StartLevelLabel.IsVisible = false);

			_world.ResetLevel();
		}

		///<summary>Event callback if lost life</summary>
		private void PlayerLostLife(int lifeLeft) {
			_world.IsEnabled = false;

			TimeLine.Wait(1.0f).Invoke(delegate() {
				if (lifeLeft > 0) {
					StartLevel(1);
				} else {
					_world.PacmanUnit.IsVisible = false;
					StartLevelLabel.IsVisible = true;
					StartLevelLabel.Location = new Vector2(400, -300);
					StartLevelLabel.Text = "Game Over";
					TimeLine.Repeat(0.5f, s => StartLevelLabel.LocationY = -300 + s.Progress * 600);
				}
			});
		}

		protected override void Load(Vortex.Resources.IResourceCollection collection) {

		}

		public override void Update(float timeDelta) {
			base.Update(timeDelta);
		}

		protected override void Draw(Canvas2D canvas) {
			//canvas.Clear(ColorU.Red);
		}
    }
}
